package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class    UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result listUsers(UserDto userDto){
        log.info("查询参数：{}",userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addUser(@RequestBody User user){
        log.info("新增用户参数：{}",user);
        userService.addUser(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        log.info("删除用户参数：{}",ids);
        userService.deleteByIds(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id为{}的用户",id);
        User userList = userService.getById(id);
        return Result.success(userList);
    }
    @PutMapping
    public Result updateUser(@RequestBody User user){
        log.info("修改用户参数：{}",user);
        userService.updateUser(user);
        return Result.success();
    }
    @GetMapping("/list")
    public Result listAll(){
        log.info("查询所有用户");
        List<User> userlist = userService.listAll();
        return Result.success(userlist);
    }
    @GetMapping("/role/{roleLabel}")
    public Result listUserByRole(@PathVariable String roleLabel){
        log.info("查询角色为{}的用户",roleLabel);
        List<User> userList = userService.listUserByRole(roleLabel);
        return Result.success(userList);
    }
    @GetMapping("/dept/{deptId}")
    public Result ListUserByDept(@PathVariable Integer deptId){
        log.info("查询部门id为{}的用户",deptId);
        List<User> userList = userService.listUserByDept(deptId);
        return Result.success(userList);
    }
}
